package com.example.demo.checkReport.controller;


import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.checkReport.entity.CheckForm;
import com.example.demo.checkReport.entity.PageCheckForm;
import com.example.demo.checkReport.service.impl.CheckFormServiceImpl;
import com.example.demo.common.ResponseData;
import com.example.demo.entryWarehouse.entity.StoreInform;
import com.example.demo.entryWarehouse.mapper.StoreInformMapper;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lbyani
 * @since 2021-05-25
 */
@RestController
@RequestMapping("/checkReport/check-form")
public class CheckFormController {

	@Autowired
	private CheckFormServiceImpl checkFormServiceImpl;
	
	@Autowired
	private StoreInformMapper storeInformMapper;
	
	/**
	 * 查询盘点详情表
	 * @param headId
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	@PostMapping("/selFromById")
	public ResponseData selCheckFormById(@RequestParam("headId") int headId, @RequestParam("currentPage") int currentPage, 
			@RequestParam("pageSize") int pageSize) {
		
		return checkFormServiceImpl.selFormById(headId, currentPage, pageSize);
		
	}
	
	/**
	 * 插入盘点表详情信息
	 * @param headId
	 * @param name
	 * @param actualNum
	 * @param price
	 * @param remark
	 * @return
	 */
	@PostMapping("/inscheckform")
	public ResponseData insCheckForm(@RequestParam("headId") int headId, @RequestParam("name") String name, 
			@RequestParam("actualNum") int actualNum, @RequestParam("price") BigDecimal price, 
			@RequestParam("remark") String remark) {
		
		// 查询库存中该物料的数量
		QueryWrapper<StoreInform> wrapperStore = new QueryWrapper<StoreInform>();
		wrapperStore.eq("name", name);
		
		StoreInform storeInform = new StoreInform();
		storeInform = storeInformMapper.selectOne(wrapperStore);
		
		// 将数据插入到 checkForm 表中
		CheckForm checkForm = new CheckForm();
		checkForm.setHeadId(headId);
		checkForm.setName(name);
		checkForm.setActualNum(actualNum);
		checkForm.setPrice(price);
		checkForm.setRemark(remark);
		checkForm.setBookNum(storeInform.getStoreNum());
		checkForm.setSpecification(storeInform.getSpecification());
		checkForm.setUnit(storeInform.getUnit());
		// 判断实有数和账面数的大小
		if (actualNum >= storeInform.getStoreNum()) {
			checkForm.setIncreNum(actualNum - storeInform.getStoreNum());
			checkForm.setDecreNum(0);
		}else {
			checkForm.setDecreNum(storeInform.getStoreNum() - actualNum);
			checkForm.setIncreNum(0);
		}
		
		try {
			checkFormServiceImpl.save(checkForm);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
			return ResponseData.buildError("数据插入失败！");
		}
		
		return ResponseData.buildOk("数据插入成功！");
	}
	
	@PostMapping("/updcheckform")
	public ResponseData updCheckForm(@RequestParam("id") int id, 
			@RequestParam("actualNum") int actualNum, @RequestParam("price") BigDecimal price, 
			@RequestParam("remark") String remark) {
		
		CheckForm checkForm = new CheckForm();
		checkForm.setId(id);
		checkForm.setActualNum(actualNum);
		checkForm.setPrice(price);
		checkForm.setRemark(remark);
		
		CheckForm oldCheckForm = new CheckForm(); 
		try {
			oldCheckForm = checkFormServiceImpl.getById(id);
			
			// 比较 新旧物料信息的 数量大小
			if(checkForm.getActualNum().equals(null)) {
				checkFormServiceImpl.updateById(checkForm);
			}else if(checkForm.getActualNum() >= oldCheckForm.getBookNum()){
				checkForm.setIncreNum(checkForm.getActualNum() - oldCheckForm.getBookNum());
				checkForm.setDecreNum(0);
				checkFormServiceImpl.updateById(checkForm);
			}else {
				checkForm.setIncreNum(0);
				checkForm.setDecreNum(oldCheckForm.getBookNum() - checkForm.getActualNum());
				checkFormServiceImpl.updateById(checkForm);
			}

		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
			return ResponseData.buildError("更新失败!");
		}
		
		return ResponseData.buildOk("更新成功！");
	}
	
	/**
	 * 根据id删除盘点表中物料信息
	 * @param id
	 * @return
	 */
	@GetMapping("/delById")
	public ResponseData delById(@RequestParam("id") int id) {
		
		try {
			checkFormServiceImpl.removeById(id);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
			return ResponseData.buildError("删除失败！");
		}
		
		return ResponseData.buildOk("删除成功! ");
	}
}

